﻿<%@ Page Language="C#" %>

<%@ Import Namespace="System.Web.Services" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head id="Head1" runat="server">
    <script src="jquery-ui-1.8.13.custom/js/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="jquery-ui-1.8.13.custom/js/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="~/jquery-ui-1.8.13.custom/css/custom-theme/jquery-ui-1.8.13.custom.css"
        type="text/css" />
    <title>PRBuilder Support Page</title>
    <script runat="server">
        private static Random _rand = new Random();

        public class Person
        {
            public string firstName;
            public string lastName;
        }

        [WebMethod]
        public static string GetStockQuoteFromPage(string symbol)
        {
            return "Hello World!";
        }

        [WebMethod]
        public static Person GetFirstNameAndLastName(string fname, string lname)
        {
            Person p = new Person { firstName = fname, lastName = lname };
            return p;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DropDownList1_SelectedIndexChanged(DropDownList1, new EventArgs());
            Response.Write(this.ClientScript.GetPostBackClientHyperlink(this.DropDownList1, ""));
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.TextBox1.Text = DropDownList1.SelectedItem.Text;
        }
    </script>
    <script type="text/javascript">
      function OnLookup() {
          var stb = document.getElementById("_symbolTextBox");
          PageMethods.GetStockQuoteFromPage(stb.value, OnLookupComplete);
      }

      function OnLookupComplete(result) {
          var res = document.getElementById("_resultLabel");
          res.innerHTML = "<b>" + result + "</b>";
      }
      
      $(function () {
          // Add the page method call as an onclick handler for the div.
          $("#btn2").click(function () {
              $.ajax({
                  type: "POST",
                  url: "webform1-cs.aspx/GetStockQuoteFromPage",
                  data: "{symbol: 'MS'}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (msg) {
                      // Replace the div's content with the page method's return.
                      alert(msg.d);
                       $("#_resultLabel").text(msg.d);
                      
                  }
              });
          });

          $("#btn3").click(function () {
              $.ajax({
                  type: "POST",
                  url: "webform1-cs.aspx/GetFirstNameAndLastName",
                  data: "{'fname':'dave', 'lname':'ward'}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (msg) {
                      // Replace the div's content with the page method's return.
                      // $("#Result").text(msg.d);
                      alert(msg.d.firstName);
                  }
              });
          });

          $("#tweetSentiment").click(function() {
              $.ajax({
                      type: "POST",
                      url: "http://tweetsentiments.com/api/analyze?q=Love+to+do+this&commit=Analyze",
                      data: "",
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
                      success: function(msg) {
                          // Replace the div's content with the page method's return.
                          alert(msg.d);
                          $("#_resultLabel").text(msg.d);

                      },
                       error:function(msg) {
                          // Replace the div's content with the page method's return.
                          alert(msg.toString());
                      } 
                  });
          }
          );
          
          $("#closeBtn").click(
                    function () {
                        alert($("#txt1").val());

                        $("#DropDownList1").val(2);

                        var op = document.getElementById("DropDownList1").options;
                        alert(op.length);
                        for (var i = 0; i < op.length; i++) {
                            // alert(op[i].text);
                            // alert(op[i].value);
                        }
                        document.getElementById("DropDownList1").selectedIndex = $("#txt1").val();
                        // $("#form2").submit();
                        $("#div1").dialog('close');
                        alert('submitting');
                        alert("<%= this.ClientScript.GetPostBackClientHyperlink(this.DropDownList1, "") %>");
                        <%= this.ClientScript.GetPostBackClientHyperlink(this.DropDownList1, "")%>;
                    });

          $("#div1").dialog({ autoOpen: false, width: 300, height: 200, buttons: { 'OK': function () {

              $(this).dialog('close');

          }
          }
          }
          );

          $("#btn1").click(function () { alert('clicked'); $("#div1").dialog('open'); $('.ui-dialog-buttonpane').remove(); });
      });
 


    </script>
    <style type="text/css">
        #closeBtn
        {
            width: 101px;
            height: 18px;
        }
    </style>
</head>
<body>
    <form id="form2" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
        <h1>
            ASP.NET AJAX Web Services: Web Service Sample Page
        </h1>
        Enter symbol:
        <asp:TextBox runat="server" ID="_symbolTextBox" />
        <br />
        <input onclick="OnLookup();" id="_lookupButton" type="button" value="Lookup" />
        <br />
        <asp:Label runat="server" ID="_resultLabel" />
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged = "DropDownList1_SelectedIndexChanged">
        <asp:ListItem Value="1">One</asp:ListItem>
        <asp:ListItem Value="2">Two</asp:ListItem>
        <asp:ListItem Value="3">Three</asp:ListItem>
        <asp:ListItem Value="4">Four</asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    <asp:TextBox ID="TextBox1" runat="server" />
    <div id="div1" style="border: 1px solid #D8EEFA">
        <input type="text" id="txt1" maxlength="25" style="border: 1px solid #D8EEFA" />
        <br />
        <input type="button" id="closeBtn" value="OK" style="border: .05em solid #D8EEFA;
            font-family: verdana; font-size: x-small;" />
    </div>
    <input type="button" id="btn1" value="test" />
    <input type="button" id="btn2" value="Click to make jQuery.ajax call to a web method. (String example)"   />
    <input type="button" id="btn3" value="Click to make jQuery.ajax call to a web method. (Object example) " />
    <br/>
    <input type="button" id="tweetSentiment" value="Call tweet sentiment" />
    </form>
</body>
</html>
